package ufabc.edu.helppo.rpc.command.impl;

import static ufabc.edu.helppo.rpc.RPCOpenSimMethods.ADMIN_CREATE_REGION;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.EXTERNAL_ADDRESS;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.LISTEN_IP;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.LISTEN_PORT;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_MASTER_FIRST;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_MASTER_LAST;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_MASTER_PASSWORD;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_NAME;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_X;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_Y;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.PERSIST;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.SERVER_PASSWORD;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

import ufabc.edu.helppo.gerador.exceptions.NenhumEspacoDisponivelException;
import ufabc.edu.helppo.persistencia.entidades.EspacoUtilizado;
import ufabc.edu.helppo.rpc.command.RPCCommand;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;
import ufabc.edu.helppo.rpc.singleton.RpcClientSingleton;

public class CriaRegiaoRPCCommandImpl extends RPCCommand {

	private String nomeDaRegiao;
	private EspacoUtilizado espaco;	
	@Override
	public boolean execute() throws XmlRpcException, IOException,
			NenhumEspacoDisponivelException {

		XmlRpcClient client = RpcClientSingleton.getInstance();
		configure(client);
		HashMap<String, String> retorno = (HashMap<String, String>)client.execute(ADMIN_CREATE_REGION, parameter());
		
		return true;//Boolean.parseBoolean(retorno.get("success"));

	}

	private List<Hashtable<String, String>> parameter() throws IOException,
			NenhumEspacoDisponivelException {
		Hashtable<String, String> parameters = new Hashtable<String, String>();


		ResourceBundle properties = HelppoProperties.getInstance();
		
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));
		parameters.put(REGION_NAME, nomeDaRegiao);
		parameters.put(REGION_MASTER_FIRST, properties.getString(REGION_MASTER_FIRST));
		parameters.put(REGION_MASTER_LAST, properties.getString(REGION_MASTER_LAST));
		parameters.put(REGION_MASTER_PASSWORD, properties.getString(REGION_MASTER_PASSWORD));
		parameters.put(LISTEN_IP, properties.getString(LISTEN_IP));
		parameters.put(EXTERNAL_ADDRESS, properties.getString(EXTERNAL_ADDRESS));
		parameters.put(REGION_X, String.valueOf(espaco.getRegion_x()));
		parameters.put(REGION_Y, String.valueOf(espaco.getRegion_y()));
		parameters.put(PERSIST, "true");
		parameters.put(LISTEN_PORT,String.valueOf(espaco.getListen_port()));

		
		List<Hashtable<String, String>> list = new ArrayList<Hashtable<String, String>>();
		list.add(parameters);

		return list;
	}

	public void setNomeDaRegiao(String nomeDaRegiao_) {
		nomeDaRegiao = nomeDaRegiao_;
	}
	public void setEspaco(EspacoUtilizado espaco_) {
		espaco = espaco_;
	}
	
}
